package tech.aistar.Conteoller;


import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.anno.FormToken;
import tech.aistar.mapper.ApplyedMapper;
import tech.aistar.mapper.BusinessmsgMapper;
import tech.aistar.mapper.HrMapper;
import tech.aistar.model.dto.Code;
import tech.aistar.model.dto.Result;
import tech.aistar.model.entity.*;
import tech.aistar.service.ICompanyService;
import tech.aistar.service.IJobService;
import tech.aistar.service.ILovejobService;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("job")
public class JobController {
    @Autowired
    private IJobService iJobService;

    @Autowired
    private HrMapper hrMapper;

    @Autowired
    private ICompanyService iCompanyService;

    @Autowired
    private BusinessmsgMapper businessmsgMapper;

    @Autowired
    private ILovejobService iLovejobService;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private ApplyedMapper applyedMapper;

    @GetMapping("companyjobs")
    public String companyJobs(HttpServletRequest req,String pageNum,String cId){
        Integer companyId= Integer.valueOf(cId);
        Integer pageN= Integer.valueOf(pageNum);
        PageInfo<Job> pageInfo=iJobService.findJobByCompanyId(pageN,companyId);
        List<Hr> hrs=hrMapper.getAllHr();
        req.setAttribute("companyId",companyId);
        req.setAttribute("hrs",hrs);
        req.setAttribute("pageInfo",pageInfo);
        return "companymsg/CompanyJobMsg";
    }

    @FormToken(create = true)
    @GetMapping("jobmsg")
    public String jobMsg(HttpServletRequest req,String jId){
        Integer jobId= Integer.valueOf(jId);
        Job j=iJobService.findJobById(jobId);
        List<String> simplere= List.of(j.getSimpleRequest().split(","));
        simplere.forEach(e-> System.out.println(e));
        List<String> welfare= List.of(j.getWelfare().split(","));
        Company c=iCompanyService.getById(iJobService.findJobById(jobId).getCompanyId());
        if(redisTemplate.hasKey("customer")){
            Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
            req.setAttribute("customer",customer);
            Applyed a=applyedMapper.getByCIdJId(customer.getId(),jobId);
            if(a!=null){
                req.setAttribute("applyedd",true);
            }else {
                req.setAttribute("applyedd",false);
            }
        }
        req.setAttribute("welfare",welfare);
        req.setAttribute("sim",simplere);
        req.setAttribute("hr",hrMapper.getById(j.getHrId()));
        req.setAttribute("businessmsg",businessmsgMapper.getById(c.getBusinessmsgId()));
        req.setAttribute("company",c);
        req.setAttribute("job",j);
        return "jobmsg/JobMsg";
    }

    @ResponseBody
    @GetMapping("addlove")
    public Result addLove(String jId){
        Integer jobId= Integer.valueOf(jId);
        Customer customer = (Customer) redisTemplate.opsForValue().get("customer");
        Lovejob lovejob =new Lovejob(null,customer.getId(),jobId,new Date());
        int state=iLovejobService.addLoveJOb(lovejob);
        if(state==1){
            return new  Result(Code.OK,"收藏成功");
        }
        return new Result(Code.BAD,"收藏失败");

    }

    @ResponseBody
    @GetMapping("dellove")
    public Result delLove(String jId){
        Integer jobId= Integer.valueOf(jId);
        Customer customer = (Customer) redisTemplate.opsForValue().get("customer");
        int state=iLovejobService.delLoveJob(customer.getId(),jobId);
        if(state==1){
            return new  Result(Code.OK,"取消收藏成功");
        }
        return new Result(Code.BAD,"取消收藏失败");

    }


}
